//
//  FANUserAccountModel.m
//  Weibo - OC
//
//  Created by user on 16/4/20.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANUserAccountModel.h"
#import "NetworkTools.h"

@implementation FANUserAccountModel

static NSString * WB_APP_KEY = @"1679233831";

static NSString * WB_REDIRECT_URI = @"http://www.baidu.com/";

static NSString * WB_APP_SECRET = @"4dd3577aaf85646ef9ae5793e6da1a0e";

+ (instancetype)sharedAccountModel{
    
    static FANUserAccountModel *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[FANUserAccountModel alloc] init];
        
    });
    
    return instance;
}

- (instancetype)init{
    
    if (self = [super init]) {
        
        self.account = [self getInfo];
        
    }
    return self;
}

- (BOOL)isLogin{
    
    if (self.account.access_token == nil) {
        
        return false;
    }
    
    if ([[NSDate date] compare:self.account.expiresDate] == NSOrderedAscending) {
        
        return true;
    }
    
    
    
    return false;
}


- (void)loadaccess_token:(NSString *)code and:(void(^)(BOOL isSuccess))finished{
    
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    
    /*
     client_id	true	string	申请应用时分配的AppKey。
     client_secret	true	string	申请应用时分配的AppSecret。
     grant_type	true	string	请求的类型，填写authorization_code
     
     grant_type为authorization_code时
     必选	类型及范围	说明
     code	true	string	调用authorize获得的code值。
     redirect_uri	true	string	回调地址，需需与注册应用里的回调地址一致。
     */
    
    NSDictionary *params = @{@"client_id":WB_APP_KEY,@"client_secret":WB_APP_SECRET,@"grant_type":@"authorization_code",@"code":code,@"redirect_uri":WB_REDIRECT_URI};
    
    [[NetworkTools sharedTools] request:FANRequestMethodPost andUrlString:urlString andParameter:params andBlock:^(NSDictionary *responseObject, NSError *error) {
        
        if (error != nil) {
            
            NSLog(@"%@",error);
            
            finished(false);
            
            return;
        }
        
        FANUserAccount *account = [FANUserAccount modelWithDict:responseObject];
        
        [self loadInfo:account and:finished];
        
    }];
}

- (void)loadInfo:(FANUserAccount *)account and:(void(^)(BOOL isSuccess))finished{
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    
    /*
     access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
     uid	false	int64	需要查询的用户ID。
     */
    
    NSDictionary *params = @{@"access_token":account.access_token,@"uid":account.uid};
    
    [[NetworkTools sharedTools] request:FANRequestMethodGet andUrlString:urlString andParameter:params andBlock:^(NSDictionary *responseObject, NSError *error) {
        
        if (error != nil) {
            
            NSLog(@"%@",error);
            
            finished(false);

            return;
        }
        
        
        account.name = responseObject[@"name"];
        
        account.avatar_large = responseObject[@"avatar_large"];
        
        self.account = account;
        
        finished(true);
        
//        NSLog(@"%@",account.name );
        
        [self saveInfo:account];
        
        
    }];
}

- (void)saveInfo:(FANUserAccount *)account{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"];
    
    [NSKeyedArchiver archiveRootObject:account toFile:path];
}

- (FANUserAccount *)getInfo{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"];
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
}

@end
